package net.FamilyTree.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.FamilyTree.entity.Event;
import net.FamilyTree.mapper.EventMapper;
import net.FamilyTree.service.EventService;
import net.FamilyTree.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EventServiceImpl extends ServiceImpl<EventMapper, Event> implements EventService {

    @Autowired
    private EventMapper eventMapper;

    /**
     * 添加事件
     * @param event 事件对象
     * @return 添加是否成功的布尔值
     */
    @Override
    public Boolean add(Event event) {
        event.setUpdatetime(new Date());
        event.setDelflag(0);
        event.setUserid(1);
        int insert = this.eventMapper.insert(event);
        // 若insert不等于1，即表示添加失败
        if (insert != 1) {
            return false;
        }
        return true;
    }

    /**
     * 获取事件列表
     * @param genealogyId 家谱ID
     * @param page 页码
     * @param size 每页大小
     * @return 包含事件列表和总记录数的PageVo对象
     */
    @Override
    public PageVo list(Integer genealogyId, Integer page, Integer size) {
        Page<Event> eventPage = new Page<>(page, size);
        QueryWrapper<Event> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("genealogy_id", genealogyId);
        Page<Event> selectPage = this.eventMapper.selectPage(eventPage, queryWrapper);
        List<Event> list = selectPage.getRecords();
        PageVo pageVo = new PageVo();
        pageVo.setData(list);
        pageVo.setTotal(selectPage.getTotal());
        return pageVo;
    }

    /**
     * 更新事件
     * @param event 事件对象
     * @return 更新是否成功的布尔值
     */
    @Override
    public Boolean update(Event event) {
        event.setUpdatetime(new Date());
        event.setDelflag(0);
        int update = this.eventMapper.updateById(event);
        if (update != 1) {
            return false;
        }
        return true;
    }

    /**
     * 删除事件
     * @param id 事件ID
     * @return 删除是否成功的布尔值
     */
    @Override
    public Boolean delete(Integer id) {
        int deleteById = this.eventMapper.deleteById(id);
        if (deleteById != 1) {
            return false;
        }
        return true;
    }
}
